package com.xydtech.ecard.merchant.service.impl;

import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.core.text.Convert;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.merchant.domain.EcardMerchantPartInfo;
import com.xydtech.ecard.merchant.mapper.EcardMerchantPartInfoMapper;
import com.xydtech.ecard.merchant.service.IEcardMerchantPartInfoService;
import com.xydtech.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 电子名片商户成员Service业务层处理
 *
 * @author xydtech
 * @date 2023-08-01
 */
@Service
public class EcardMerchantPartInfoServiceImpl implements IEcardMerchantPartInfoService
{
    @Autowired
    private EcardMerchantPartInfoMapper ecardMerchantPartInfoMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询电子名片商户成员
     *
     * @param id 电子名片商户成员主键
     * @return 电子名片商户成员
     */
    @Override
    public EcardMerchantPartInfo selectEcardMerchantPartInfoById(Long id, String salt)
    {
        EcardMerchantPartInfo merchantPartInfo = ecardMerchantPartInfoMapper.selectEcardMerchantPartInfoById(id);
        if (merchantPartInfo != null) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(merchantPartInfo.getPhone())) {
                    merchantPartInfo.setPhone(AESEncryptionUtils.decrypt(merchantPartInfo.getPhone(), salt));
                }
                if (!StringUtils.objIsNullOrEmpty(merchantPartInfo.getIdCard())) {
                    merchantPartInfo.setIdCard(AESEncryptionUtils.decrypt(merchantPartInfo.getIdCard(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return merchantPartInfo;
    }

    /**
     * 查询电子名片商户成员列表
     *
     * @param ecardMerchantPartInfo 电子名片商户成员
     * @return 电子名片商户成员
     */
    @Override
    public List<EcardMerchantPartInfo> selectEcardMerchantPartInfoList(EcardMerchantPartInfo ecardMerchantPartInfo, String salt)
    {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardMerchantPartInfo.getPhone())) {
                ecardMerchantPartInfo.setPhone(AESEncryptionUtils.encrypt(ecardMerchantPartInfo.getPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardMerchantPartInfo.getIdCard())) {
                ecardMerchantPartInfo.setIdCard(AESEncryptionUtils.encrypt(ecardMerchantPartInfo.getIdCard(), salt));
            }
            List<EcardMerchantPartInfo> merchantPartInfoList = ecardMerchantPartInfoMapper.selectEcardMerchantPartInfoList(ecardMerchantPartInfo);
            if (merchantPartInfoList.size() > 0) {
                for (EcardMerchantPartInfo merchantPartInfo : merchantPartInfoList) {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(merchantPartInfo.getPhone())) {
                        merchantPartInfo.setPhone(AESEncryptionUtils.decrypt(merchantPartInfo.getPhone(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(merchantPartInfo.getIdCard())) {
                        merchantPartInfo.setIdCard(AESEncryptionUtils.decrypt(merchantPartInfo.getIdCard(), salt));
                    }
                }
            }
            return merchantPartInfoList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 新增电子名片商户成员
     *
     * @param ecardMerchantPartInfo 电子名片商户成员
     * @return 结果
     */
    @Override
    public int insertEcardMerchantPartInfo(EcardMerchantPartInfo ecardMerchantPartInfo, String salt)
    {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardMerchantPartInfo.getPhone())) {
                ecardMerchantPartInfo.setPhone(AESEncryptionUtils.encrypt(ecardMerchantPartInfo.getPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardMerchantPartInfo.getIdCard())) {
                ecardMerchantPartInfo.setIdCard(AESEncryptionUtils.encrypt(ecardMerchantPartInfo.getIdCard(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardMerchantPartInfoMapper.insertEcardMerchantPartInfo(ecardMerchantPartInfo);
    }

    /**
     * 修改电子名片商户成员
     *
     * @param ecardMerchantPartInfo 电子名片商户成员
     * @return 结果
     */
    @Override
    public int updateEcardMerchantPartInfo(EcardMerchantPartInfo ecardMerchantPartInfo, String salt)
    {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardMerchantPartInfo.getPhone())) {
                ecardMerchantPartInfo.setPhone(AESEncryptionUtils.encrypt(ecardMerchantPartInfo.getPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardMerchantPartInfo.getIdCard())) {
                ecardMerchantPartInfo.setIdCard(AESEncryptionUtils.encrypt(ecardMerchantPartInfo.getIdCard(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardMerchantPartInfoMapper.updateEcardMerchantPartInfo(ecardMerchantPartInfo);
    }

    /**
     * 批量删除电子名片商户成员
     *
     * @param ids 需要删除的电子名片商户成员主键
     * @return 结果
     */
    @Override
    public int deleteEcardMerchantPartInfoByIds(String ids)
    {
        return ecardMerchantPartInfoMapper.deleteEcardMerchantPartInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除电子名片商户成员信息
     *
     * @param id 电子名片商户成员主键
     * @return 结果
     */
    @Override
    public int deleteEcardMerchantPartInfoById(Long id)
    {
        return ecardMerchantPartInfoMapper.deleteEcardMerchantPartInfoById(id);
    }

    /**
     * 查询导出的商户成员信息
     * @param merchantPartInfo
     * @return
     */
    @Override
    public List<EcardMerchantPartInfo> selectEcardMerchantPartInfoListByExport(EcardMerchantPartInfo merchantPartInfo, String salt) {
        try {
            SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
            merchantPartInfo.setDictDeptId(sysDept.getDeptId());
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(merchantPartInfo.getPhone())) {
                merchantPartInfo.setPhone(AESEncryptionUtils.encrypt(merchantPartInfo.getPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(merchantPartInfo.getIdCard())) {
                merchantPartInfo.setIdCard(AESEncryptionUtils.encrypt(merchantPartInfo.getIdCard(), salt));
            }
            List<EcardMerchantPartInfo> merchantPartInfoList = ecardMerchantPartInfoMapper.selectEcardMerchantPartInfoListByExport(merchantPartInfo);
            if (merchantPartInfoList.size() > 0) {
                for (EcardMerchantPartInfo ecardMerchantPartInfo : merchantPartInfoList) {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(ecardMerchantPartInfo.getPhone())) {
                        ecardMerchantPartInfo.setPhone(AESEncryptionUtils.decrypt(ecardMerchantPartInfo.getPhone(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(ecardMerchantPartInfo.getIdCard())) {
                        ecardMerchantPartInfo.setIdCard(AESEncryptionUtils.decrypt(ecardMerchantPartInfo.getIdCard(), salt));
                    }
                }
            }
            return merchantPartInfoList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据id集合查询成员信息
     * @param ids
     * @return
     */
    @Override
    public List<EcardMerchantPartInfo> selectEcardMerchantPartInfoListByIds(String ids, String merchantType, String salt) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        List<EcardMerchantPartInfo> merchantPartInfoList = ecardMerchantPartInfoMapper.selectEcardMerchantPartInfoListByIds(Convert.toStrArray(ids), merchantType, sysDept.getDeptId());
        if (merchantPartInfoList.size() > 0) {
            for (EcardMerchantPartInfo merchantPartInfo : merchantPartInfoList) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(merchantPartInfo.getPhone())) {
                        merchantPartInfo.setPhone(AESEncryptionUtils.decrypt(merchantPartInfo.getPhone(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(merchantPartInfo.getIdCard())) {
                        merchantPartInfo.setIdCard(AESEncryptionUtils.decrypt(merchantPartInfo.getIdCard(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return merchantPartInfoList;
    }
}
